﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;
using V5_Common;
using V5_BLL;

namespace V5_EShop._Class.Data {
    public class V5ArticleHelper : IV5Helper {
        private static readonly V5ArticleHelper m_Instance = new V5ArticleHelper();
        /// <summary>
        /// 
        /// </summary>
        public static V5ArticleHelper Instance {
            get { return m_Instance; }
        }
        BLLArticle bllArticle = new BLLArticle();
        BLLArticleClass bllArticleClass = new BLLArticleClass();

        private HtmlString _Pager = new HtmlString("");

        public HtmlString Pager {
            get { return _Pager; }
            set { _Pager = value; }
        }
        #region 文章操作
        /// <summary>
        /// 获取文章内容
        /// </summary>
        /// <param name="id">文章ID</param>
        /// <returns></returns>
        public DataRow GetDataRow(object oID) {
            string id = InputHelper.SetString(oID);
            DataTable dt = bllArticle.GetList(" And ID=" + id).Tables[0];
            if (dt.Rows.Count > 0) {
                return dt.Rows[0];
            }
            else {
                return dt.NewRow();
            }
        }
        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <param name="ClassID"></param>
        /// <param name="isSub"></param>
        /// <param name="what"></param>
        /// <param name="where"></param>
        /// <param name="orderby"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public DataRowCollection GetList(int ClassID, bool isSub, string what, string where, string orderby, int limit) {
            DataRowCollection drc = null;
            string ClassIDList = ClassID == 0 ? string.Empty : ClassID.ToString() + ",";
            if (isSub) {
                foreach (DataRow dr in GetClassList("ClassID", "ParentID=" + ClassID, string.Empty, 0)) {
                    ClassIDList += dr["ClassID"].ToString() + ",";
                }
            }
            if (ClassIDList.Length > 0) {
                ClassIDList = ClassIDList.Remove(ClassIDList.Length - 1);
            }

            if (!string.IsNullOrEmpty(where)) {
                where += " And ClassID in(" + ClassIDList + ") ";
            }
            else {
                where += " ClassID in(" + ClassIDList + ") ";
            }
            drc = V5.SQL.Query_By(what, "K_Article", where, orderby, limit).Rows;
            return drc;
        }
        #endregion
        #region 分类
        /// <summary>
        /// 获取分类单个属性
        /// </summary>
        /// <param name="oClassID"></param>
        /// <returns></returns>
        public DataRow GetClassSingle(object oClassID) {
            return V5.SQL.Query_By("*", "K_ArticleClass", " ClassID=" + oClassID, "", 1).Rows[0];
        }
        /// <summary>
        /// 获取分类信息
        /// </summary>
        /// <param name="what"></param>
        /// <param name="where"></param>
        /// <param name="orderby"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public DataRowCollection GetClassList(string what, string where, string orderby, int limit) {
            return V5.SQL.Query_By(what, "K_ArticleClass", where, orderby, limit).Rows;
        }
        #endregion

        #region 
        public DataTable Query_By(string what, string where, string orderby, int limit) {
            return bllArticle.GetList(what, limit, where, orderby).Tables[0];
        }

        public DataTable Query_By_List(string what, string where, string orderby, int limit) {
            return bllArticleClass.GetList(what, limit, where, orderby).Tables[0];
        }

        public DataTable Query_By_Pager(string what, string where, string orderby, int limit, int pageIndex, string UrlList) {
            int RecordCount = 0;
            DataTable dtPager = bllArticle.GetList(what, limit, pageIndex, where, orderby, ref RecordCount).Tables[0];
            Easp.Pager.RecordCount = RecordCount;
            Easp.Pager.CurrentPageIndex = pageIndex;
            Easp.Pager.PageSize = Easp.Str.SetNumber(limit); ;
            Easp.Pager.PageUrlFormat = UrlList.Replace("@pageid@", "{0}");
            this.Pager = new HtmlString(Easp.Pager.Display());
            return dtPager;
        }

        public DataRow Get_By(string ID) {
            using (DataTable dt = bllArticle.GetList(" And ID='" + ID + "'").Tables[0]) {
                if (dt != null && dt.Rows.Count > 0) {
                    return dt.Rows[0];
                }
                return null;
            }
        }

        public DataRow Get_ByClassID(string ClassID)
        {
            using (DataTable dt = bllArticle.GetList(" And ClassID='" + ClassID + "'").Tables[0])
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    return dt.Rows[0];
                }
                return null;
            }
        }


        public DataRow Get_By_List(string ClassID) {
            using (DataTable dt = bllArticleClass.GetList(" And ID='" + ClassID + "'").Tables[0]) {
                if (dt != null && dt.Rows.Count > 0) {
                    return dt.Rows[0];
                }
                return null;
            }
        }

        public string UrlView(object attributes) {
            throw new NotImplementedException();
        }

        public string UrlList(object attributes) {
            throw new NotImplementedException();
        }
        #endregion
    }
}